{include file="public/header-static"/}
{include file="public/sidebar"/}
<style>
    .activeBtn {
        font-size: 14px;
        border-radius: 3px;
        background-color: #F0F0F0FF;
        color: #999999FF;
        display: grid;
        place-items: center;
        cursor: pointer;
        line-height: 32px;
        height: 32px;
        padding-left: 10px;
        padding-right: 10px;
        color: #fff !important;
        background-color: #3F6EFFFF !important;
    }
</style>
<div id="main">

    {include file="public/header"/}

    <main class="main-content">

        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="app-block">
                                <div class="app-content">
                                    <div class="app-action">
                                        <div class="action-left">
                                            <div class="list-title">
                                                <div class="item activeBtn" onclick="add()">✚ 发布</div>


                                            </div>
                                        </div>
                                        <div class="action-right">
                                            <div class="input-group">
                                                <input type="text" name="keywords" id="keywords" autofocus="autofocus"
                                                       value="{$Request.get.keywords}" class="form-control"
                                                       placeholder="分类名称"
                                                       aria-describedby="button-addon1">
                                                <div class="input-group-append">
                                                    <button class="btn btn-outline-light" type="submit"
                                                            id="button-addon1">
                                                        <i data-feather="search"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <table id="linkstable" lay-filter="linkstable"></table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </main>
    {include file="public/footer"/}
</div>

<script type="text/html" id="barDemo">
    <button onclick="edit({{d.id}})" class="btn btn-sm btn-outline-secondary">编辑</button><br>
    <button onclick="del({{d.id}})" class="btn btn-sm btn-outline-secondary">删除</button>
</script>
<script src="__STATIC__/assets/js/custom.js"></script>

<script>
    // 添加
    function add() {
        window.location.href = '{:url("/lore/loreCateEdit")}';
        // layer.prompt({title: '添加'}, function(value, index){
        //     console.log(value);
        //     $.ajax({
        //         url:'{:url("/article/articleCateEdit")}',
        //         type:'post',
        //         data:{name:value},
        //         dataType:'json',
        //         success:function (res) {
        //             // console.log(res,res.msg);
        //             if (res.code != 1) {
        //                 toastr.error(res.msg);
        //             } else {
        //                 // toastr.success(res.msg);
        //                 window.location.reload();
        //             }
        //         }
        //     });
        //     layer.close(index);
        // });
    }

    // 编辑
    function edit(id) {
        window.location.href = '{:url("/lore/loreCateEdit")}?id=' + id;
        // layer.prompt({title: '编辑',value: data.name}, function(value, index){
        //     console.log(value);
        //     $.ajax({
        //         url:'{:url("/article/articleCateEdit")}',
        //         type:'post',
        //         data:{id:data.id,name:value},
        //         dataType:'json',
        //         success:function (res) {
        //             // console.log(res,res.msg);
        //             if (res.code != 1) {
        //                 toastr.error(res.msg);
        //             } else {
        //                 // toastr.success(res.msg);
        //                 window.location.reload();
        //             }
        //         }
        //     });
        //     layer.close(index);
        // });
    }

    // 删除
    function del(id) {
        layer.confirm('删除该数据?', {icon: 3, title: '提示'}, function (index) {
            $.post("{:url('/lore/loreCateDel')}", {id: id}, function (data) {
                if (data['code'] != 1) {
                    toastr.error(data['msg']);
                } else {
                    window.location.reload();
                }
            });
            layer.close(index);
        });
    }

    // layui插件
    layui.use('table', function () {
        var table = layui.table
            , form = layui.form;
        //第一个实例
        table.render({
            elem: '#linkstable'
            , skin: 'line' //行边框风格
            , even: true //开启隔行背景
            , url: '{:url("/lore/loreCate")}'
            , method: 'post'
            , page: true //开启分页
            , cols: [[
                {field: 'id', title: 'ID'}
                , {field: 'name', title: '分类名称'}
                , {field: 'sort', title: '排序'}
                , {field: 'create_time', title: '添加时间'}
                , {field: 'rightbtn', title: '操作', width: 200, toolbar: '#barDemo'}
            ]],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count,
                    "data": res.data.list
                };
            }
        });

        // 搜索
        $('#button-addon1').on('click', function () {
            var keywords = $("#keywords").val();
            table.reload('linkstable', {
                url: '{:url("/lore/loreCate")}'
                , where: {
                    keywords: keywords // 搜索参数
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        form.on('switch(switch_status)', function (obj) {
            $.post("{:url('/power/fieldchange')}", {id: obj.value, field: 'status'}, function () {
            });
        });
    });
</script>